package com.zmn.uac.client;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.tapi.common.wechat.WechatAccessToken;
import com.zmn.tapi.dubbo.DubboConsts;
import com.zmn.tapi.dubbo.interfaces.wechat.WechatOffiaccountRemoteService;
import com.zmn.uac.UacException;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

/**
 * @title: 微信公众号Dubbo 接口
 * @author:Rananquan
 * @Date:2022/6/9 17:00
 */
@Slf4j
@Component
public class WeChatOfficiantClient {

    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    WechatOffiaccountRemoteService wechatOffiaccountRemoteService;

    /**
     * 获取Access token
     * @param appId 第三方用户唯一凭证
     * @param appSecret 第三方用户唯一凭证密钥
     * @return
     */
    public WechatAccessToken token(String appId,String appSecret){
        log.info("获取Access token 入参, appId：{}，appSecret：{}",appId,appSecret);
        ResponseDTO<WechatAccessToken> tokenResponse = wechatOffiaccountRemoteService.token(appId, appSecret);
        if(!tokenResponse.isSuccess()){
            log.error("获取Access token 请求失败，失败原因：{}",tokenResponse.getMessage());
            throw new UacException(tokenResponse.getMessage());
        }

        WechatAccessToken wechatAccessToken = tokenResponse.getData();
        log.info("获取Access token 返回response：[{}]",wechatAccessToken);

        return wechatAccessToken;
    }
}
